package com.tmc.service.intcar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intcar.EthConfirmOrderReq;
import com.tmc.constant.IntcarOrderConstant;
import com.tmc.entity.intcar.IntcarOrder;
import com.tmc.util.ObjectUtil;

/**
 * @author zhang
 * @date 2017年12月29日 下午3:04:12 
 */
@Service
public class IntcarConfirmOrderServiceImpl {
	
	private Log log = LogFactory.getLog(this.getClass());
	
	@Autowired
	private IntcarOrderServiceImpl orderService;
	
	public CommonResult confirming(ContextInfo context, EthConfirmOrderReq req){
		CommonResult check = this.check(req);
		if(!check.isSuccess()){
			return check;
		}
		IntcarOrder dbOrder = orderService.getById(context, req.getOrderID());
		if(dbOrder == null){
			return CommonResult.makeFail("ID为"+req.getOrderID()+"的订单不存在！");
		}
		if(!dbOrder.getVendorOrderID().equals(req.getVendorOrderID())){
			return CommonResult.makeFail("供应商ID错误！");
		}
		//VendorService service = ServiceCenter.getService(dbOrder.getVendor());
		try {
			if(req.getConfirmType() == IntcarOrderConstant.ORDER_CONFIRMTYPE_ACCEPTED){
				dbOrder.setOrderStatus(IntcarOrderConstant.ORDER_STATUS_PENDINGCAR);
			}else{
				dbOrder.setOrderStatus(IntcarOrderConstant.ORDER_STATUS_CANCELING);
			}
			
			CommonResult result = orderService.updateOrderStatus(context, dbOrder);
			return result;
		} catch (Exception e) {
			log.error("订单确认出错！", e);
		}
		return CommonResult.makeFail("订单确认出错！");
	}
	
	private CommonResult check(EthConfirmOrderReq req){
		if(req.getOrderID() == null){
			return CommonResult.makeFail("orderID不能为空！");
		}
		if(ObjectUtil.noText(req.getVendorOrderID())){
			return CommonResult.makeFail("供应商ID不能为空！");
		}
		if(req.getConfirmType() == null){
			return CommonResult.makeFail("订单确认类型不能为空！");
		}
		if(req.getConfirmType() != 1 || req.getConfirmType() != 2){
			return CommonResult.makeFail("订单确认类型不存在！");
		}
		return CommonResult.makeSuccess();
	}
}
